<?php
/**
 * 用户表数据操作层
 * @author yangchao
 * @package user
 * @copyright yangchao
 * @version 1.0
 * @code-encode utf-8
 * @data-encode 2011-01-17
 */


define("CACHE_PREFIX_TUCAO","tucao_");


class db_tucao extends db_base {
    
	var $table_name = "tucao";
	
	function db_tucao()
	{
		parent::db_base();
	}
	
	/**
	 * 前台取单条信息
	 * @param $uid
	 */
	function get_tucao_item_by_id($id)
	{
		$id = intval($id);
		$mc = mem_cache::get_instance();
		$key = CACHE_PREFIX_USER_ITEM."-get_tucao_item_by_id-{$id}";
		$cache = $mc->get($key);
		if ($cache === false)
		{
			$content = $this->get_alllist($this->table_name,1,1,"","id='{$id}' and status=1");
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content[0];
		
	}
	
	
		/**
	 * 前台取单条信息
	 * @param $ids array
	 */
	function get_tucao_by_ids($ids)
	{
		$ids = join(",",$ids);
		$mc = mem_cache::get_instance();
		$key = CACHE_PREFIX_USER_ITEM."-get_tucao_by_ids-{$ids}";
		$cache = $mc->get($key);
		if ($cache === false)
		{
			$ids = mysql_escape_string($ids);
			$content = $this->get_alllist($this->table_name,"","","","id  in ( {$ids} ) and status=1");
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content;
		
	}
	
	

	
	/**
	 * 根据用户名取数据
	 * @param $user_name
	 * @param $password
	 */
	function get_tucao_list($type="",$p=1,$pcount=10)
	{
		$type = intval($type);
		$pcount = intval($pcount);
		$p = intval($p);
		
		$mc = mem_cache::get_instance();
		$key = CACHE_PREFIX_USER_ITEM."-get_tucao_list-{$type}-{$p}-{$pcount}";
		$cache = $mc->get($key);
		if ($cache === false)
		{
			if($type)$where_array[] = "type = {$type}";
			
			$where_array[] = "status = 1";
			$where = join(" and ",$where_array);
			
			$content = $this->get_alllist($this->table_name,$p,$pcount,"",$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		return $content;
	}
	
	/**
	 * 根据用户名取数据
	 * @param $user_name
	 * @param $password
	 */
	function get_tucao_count($type="")
	{
		$type = intval($type);
		$pcount = intval($pcount);
		$p = intval($p);
		
		$mc = mem_cache::get_instance();
		$key = CACHE_PREFIX_USER_ITEM."-get_tucao_count-{$type}";
		$cache = $mc->get($key);
		if ($cache === false)
		{
			if($type)$where_array[] = "type = {$type}";
			
			$where_array[] = "status = 1";
			$where = join(" and ",$where_array);
			
			$content = $this->get_listcount($this->table_name,$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		return $content;
	}
	
	
	
	/**
	 * 用户注册
	 * @param $user_name
	 * @param $password
	 */
	function insert_tucao($uid,$user_nickname,$type,$title,$content)
	{
		// 插入数据
		$ary = array();

		$ary["uid"] = $uid;
		$ary["user_nickname"] = $user_nickname;
		$ary["type"] = $type;
		$ary["title"] = $title;
		$ary["content"] = $content;
		

		$ary["create_time"] = Date::get_date_time();
		$ary["create_ip"] = IP::get_client_ip_long();
		$ary["update_time"] = Date::get_date_time();
		$ary["update_ip"] = IP::get_client_ip_long();
		$ary["status"] = 1;
		
		$ret = $this->insert($this->table_name, $ary);
		if ($ret)
		{
			/*$memcache = mem_cache::get_instance();
			$memcache->delete($key);*/
		}
		else
		{
			return false;
		}


		return $this->get_lastinsertid();
	}
	
	

}



